package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by wolfcode-lanxw
 */
public class UserContext {
    public static final String USER_SESSION_NAME = "USER_IN_SESSION";
    public static final String PERMISSION_SESSION_NAME = "PERMISSION_IN_SESSION";
    public static HttpSession getSession(){
        ServletRequestAttributes attrs =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }
    public static Employee getCurrentUser(){
        return (Employee) getSession().getAttribute(UserContext.USER_SESSION_NAME);
    }
    public static void setCurrentUser(Employee currentUser){
       getSession().setAttribute(UserContext.USER_SESSION_NAME,currentUser);
    }
    public static List<Permission> getCurrentPermissions(){
        return (List<Permission>) getSession().getAttribute(UserContext.PERMISSION_SESSION_NAME);
    }
    public static void setCurrentPermissions(List<Permission> currentPermissions){
        getSession().setAttribute(UserContext.PERMISSION_SESSION_NAME,currentPermissions);
    }
}
